<?php

declare(strict_types=1);

namespace app\admin\controller\order;

use api\channel\jutuike\AroundUnion;
use app\admin\controller\Controller;
use app\common\service\around\JtkAroundService;
use app\event\SupplierCreateOrder;
use app\event\SupplierNotifyFail;
use app\model\CouponOrder;
use think\Request;

/**
 * @apiDefine oilAdmin 管理后台
 */
class Coupon extends Controller
{
    public function list(Request $request)
    {
        $data = input('post.');
        if (!empty($data['is_export'])) {
            return CouponOrder::getAdminList($data);
        }

        $list = CouponOrder::getAdminList($data);
        return success('操作成功', $list);
    }

    /**
     * 权益订单列表
     * @return \think\response\Json|void
     */
    public function couponOrderList() {
        $data = input('post.');
        $data['platform'] = CouponOrder::C_PLATFORM_ADMIN;
        $couponOrderModel = new CouponOrder();
        $list = $couponOrderModel->couponOrderList($data);
//        if ($list['status']) {  //导出会失败
//            return success('操作成功', $list['data']);
//        }
//        $msg = $list['msg'] ?? '操作失败';
        return $list;
    }

    /**
     * 聚推客退款测试
     * @return \think\response\Json
     */
    public function refundJtk() {
        $params = input('post.');
        $result = JtkAroundService::getInstance()->refundOrderJtk($params);
        return success('成功！', $result);
    }

    /**
     * 人工确认-订单操作
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderStatus() {
        $data = input('');
        $orderStatus = $data['order_status'] ?? '';
        $id = $data['id'] ?? '';
        if (empty($id)) {
            return fail('订单参数id不能为空');
        }
        if (empty($orderStatus)) {
            return fail('订单参数order_status不能为空');
        }
        $couponOrder = CouponOrder::query()->where('id', $id)->find();
        if (empty($couponOrder)) {
            return fail('订单不存在');
        }
        if ($orderStatus == 'confirm_success') {
            event(new SupplierCreateOrder($couponOrder));
            return success('订单已提交');
        }


        if ($orderStatus == 'confirm_fail') {

            if ($couponOrder->serv_type = PRODUCT_AROUND_SHOP_SERV
                && $couponOrder->type = CouponOrder::TYPE_COUPON_SELF_EMPLOYED) {

                if ($couponOrder->supplier_status == CouponOrder::SUPPLIER_STATUS_REFUNDING) {
                    return fail('退款中，请稍后再来查看');
                }

                // 周边游需要推送给聚推客退款 ，周边游只能推送一次
                $extData = $couponOrder->ext_data ?? [];
                $refundOrderRes = $extData[JtkAroundService::ORDER_REFUND_ORDER_RES] ?? [];
               if (empty($refundOrderRes)) {
                   $result = JtkAroundService::getInstance()->refundOrder($couponOrder);
                   if (!$result['status']) {
                       return fail('订单已操作退款,退款异常:' . $result['msg']);
                   }
               }
            }

            event(new SupplierNotifyFail($couponOrder));
            return success('订单已操作退款,退款成功后金额原路返回');

        }

        return success('订单操作');

    }


}
